﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS;
using System.Data;

namespace Photo
{
    public partial class listIMG : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadImg();
            }

        }

        public void gvListImg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListImg.PageIndex = e.NewPageIndex;
            LoadImg();
        }
        public void gvListImg_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                Xoafile(sender, e);
                new IMGBL().Delete(new IMGDO { ImgID = Convert.ToInt32(gvListImg.DataKeys[e.RowIndex].Value) });
                Functions.Alert("Xóa thành công!", Request.Url.ToString());
            }
            catch (Exception i)
            {
                Functions.Alert(i.Message, Request.Url.ToString());
            }

        }

       

        private void Xoafile(object sender, GridViewDeleteEventArgs e)
        {
            IMGDO Img = new IMGBL().Select(new IMGDO { ImgID = Convert.ToInt32(gvListImg.DataKeys[e.RowIndex].Value) });
            string duongdan = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), Img.Url);
            if (System.IO.File.Exists(duongdan))
            {
                System.IO.File.Delete(duongdan);
            }

          

        }
       
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Ultility.RedirectPage("Img.aspx");
        }

        private void LoadImg()
        {

            gvListImg.DataSource = new IMGBL().SelectbyAlbum(int.Parse(Request.QueryString["AlbumID"].ToString()));
            gvListImg.DataBind();
        }

        protected void uploadAnh_Click(object sender, GridViewDeleteEventArgs e)
        {
            Functions.Alert(gvListImg.DataKeys[gvListImg.SelectedIndex].Value.ToString(), Request.Url.ToString());
        }
    }
}